function _curTime()
 {
    var now = new Date();
    var yy = now.getFullYear();
    var month = now.getMonth()+1;
    var day = now.getDate();
    var hh = now.getHours();
    var mm = now.getMinutes();
    var ss = now.getSeconds();

    var clock = yy+'-';
     if (month < 10) {
	clock += '0';
     }
     clock += month+'-';
     
     if (day < 10) {
	clock += '0';
     }
     clock += day+' ';
     
      if (hh < 10) {
	clock += '0';
     }
     clock += hh+':';

    if (mm < 10) {
	clock += '0';
    }
    clock += mm+':';

    if (ss < 10){
	clock += '0';
     }
    clock += ss;
    return(clock);
}

/***
 *返回一个单精度的字符串
 *
 ****/

function changeTwoDecimal_f(x)
{
   var f_x = parseFloat(x);
   if(x==0){
	  return 0;
   }
   if(x==100){
	  return 100;
   }
   var f_x = Math.round(x*10)/10;
   var s_x = f_x.toString();
   var pos_decimal = s_x.indexOf('.');
   if (pos_decimal < 0)
   {
      pos_decimal = s_x.length;
      s_x += '.';
   }

  	while (s_x.length <= pos_decimal + 1)
   {
      s_x += '0';
   }
   return s_x;
}

/**
 * 课程详情页所需效果
 * @author Jay
 */
/*
 * 发表对课程的评论
 */
$(function(){
	$('.hot_course,.hot_teacher').find('li').hover(
		function(){
			$(this).addClass('active').siblings('li').removeClass('active');
		},
		function(){
			$(this).removeClass('active');
		}
	);
	$('.hot_course').eq(1).addClass('readed');
	$('.hot_course').eq(1).addClass('readed');
	/* upload:2010-11-16 By:Jay */
	//progressTips($('#learn_fettle ol'),$('#learn_fettle ul'),$('#learn_fettle'))
	/* upload:2010-11-11 By:Jay */
	//progressTipsSetArrow($('#learn_fettle ol'));
	$('.hot_course a').wordLimit();
	
	// 宣传视频自动播放
	if($('.video_area .video img').length>0){
		vShow($('.video_area .video img').attr('id'));
	}
});
// 宣传视频自动播放
function vShow(id,w,h){
	var _id=id.split('_video_')[1],
		_tp=id.split('_video_')[0]; 
	
	videoShow({
		auto:true,            //自动播放
		id:_id,                   //课程ID 
		width:w||'500',         //宽度
		height:h||'310',        //高度
		picUrl:'',
		hitID:'',
		type:_tp,
		FlashID:id   	//容器的id(<div id="focusVideo"><img .... /></div>)
	});
}
function kcCommentAdd (id,d) {
    var content = $("#comment_content").val();
	//var course_mark = $(".course-mark-on").width()/26;
	var course_mark = $('#comment_mark_valbox').val();
	//alert(course_mark);
	//course_mark=course_mark==0?5:course_mark;
    if ($.trim(content) == '') {
        alert("请填写评论内容！");
        $("#comment_content").focus();
        return false;
    }
	if($.trim(content).length<6 ){
	 alert("您的评论长度不符合要求，请输入不少于6个字！");
        $("#comment_content").focus();
        return false;
	}
	else{
		if($.trim(content).length>200 ){
			alert("您的评论长度不符合要求，请输入不多于200个字！");
			$("#comment_content").focus();
			return false;
		}else{
			$.ajax({
				url :'/course/commentadd/',
				type:'post',
				cache:false,
				data:'id='+id+'&content='+content+'&coursemark='+course_mark,
				dataType:'html',
				success: function (msg) {
					switch(msg){
						case "1":
							if(d=='list'){
								alert("评论成功！");
								ajax_courseall(0);
							}else{
								if(d=='imgs'){
									var sumnums = parseInt(document.getElementById("sumnums").innerHTML);
									document.getElementById("sumnums").innerHTML = sumnums+1;
									var index = 0;
									$($('.js_tab_tit_new li')[0]).addClass('current').siblings().removeClass('current');
									alert("评论成功！");
									ajax_course(0);
								}else{
									alert("评论成功！");
									window.location.href="/kc/"+d+".html";
								}
							}
							break;
						case "2":
							alert("评论失败！");
							break;
						case "3":
							var pos=getElementPos("img"+id);
							$("#userBox").show();
							$("#username").focus();
							var b = document.getElementById("userBox");
							var _x,_y;
							pos=d?getElementPos(d+id):getElementPos("img"+id);
							//							if(d){
							//								pos=getElementPos(d+id);
							//							}else{
							//								pos=getElementPos("img"+id);
							//							}
							_x= pos.x-400+"px";
							_y= pos.y-160+"px";
							b.style.left = _x;
							b.style.top = _y;
							break;
						case "4":
							alert("您已经评论过，不能再评论！");
							break;
						case "5":
							alert("请您先打分！");
							break;
						case "6":
							alert("请您先登录，再进行评论！");
							break;
						case "7":
							if(d=='list'){
								alert("评论成功！");
								ajax_courseall(0);
							}else{
								if(d=='imgs'){							
									var sumnums = parseInt(document.getElementById("sumnums").innerHTML);							
									document.getElementById("sumnums").innerHTML = sumnums+1;
									var index = 0;
									$($('.js_tab_tit_new li')[0]).addClass('current').siblings().removeClass('current');
									alert("评论成功！");
									ajax_course(0);
								}else{
									alert("评论成功！");
									window.location.href="/kc/"+d+".html";
								}
							}
							break;
						case "8":
							if(d=='list'){
								alert("评论成功！");
								ajax_courseall(0);
							}else{
								if(d=='imgs'){									
									var sumnums = parseInt(document.getElementById("sumnums").innerHTML);									
									document.getElementById("sumnums").innerHTML = sumnums+1;									
									var index = 0;
									$($('.js_tab_tit_new li')[0]).addClass('current').siblings().removeClass('current');
									alert("评论成功！");
									ajax_course(0);
								}else{
									alert("评论成功！");
									window.location.href="/kc/"+d+".html";
								}
							}
							break;
						default:
							alert("你输入的字符: <<"+ msg +">> 非法");
							break;
					}
				}
			})
		}  	
    }
};

/*
 *  发表对课程的提问
 *  subin
 */
function kcQuestionAdd (id) {
    var content = $("#question_content").val();
    if ($.trim(content) == '') {
        alert("请填写咨询内容！");
        $("#question_content").focus();
        return false;
    }else{
    	$.ajax({
    		url :'http://www.xueersi.com/course/questionadd/',
    		type:'post',
                cache:false,
                data:'id='+id+'&content='+content,
    		dataType:'html',
    		success: function (msg) {
                    switch(msg){
                        case "1":
							var _date= _curTime();
							var _html = '<div class="kc_bgbox borderTop_none course_question">'
							            +'      <div class="student">'
										+'			<p class="reviewDate"><span class="kc_font3"> '+_date+'</span></p>'
										+'			<p class="reviewContent"><span class="content">咨询内容：</span>'+content+'</p>'
							            +'            <div class="clear"></div>'
							            +'    </div>'
							            +' </div>';
							var _list=$('.course_question');
							if(_list.length>4){
							    _list.last().remove();
							}
							$("#course_qes").hide();
							$('.gird_advisory .kc_plmore').before(_html);
							$('#question_input').hide();
                            break;
                        case "2":
                            alert("发表咨询失败！");
                            break;
                        case "3":
                            var pos=getElementPos("img"+id);
                            $("#userBox").show();
                            $("#username").focus();
                            var b = document.getElementById("userBox");
                            b.style.left = pos.x+260+"px";
                            b.style.top = pos.y+"px";
                            break;
                        case "4":
                            alert("一分钟内不能重复咨询！");
                            break;
                        default:
                            alert("你输入的字符: <<"+ msg +">> 非法");
                            break;
                    }
    		}
    	})
    }
};

	function kcCommentDc (id,typeid) {
    	$.ajax({
    		url :'/course/commentdc/',
    		type:'post',
                cache:false,
                data:'id='+id+'&typeid='+typeid,
    		dataType:'html',
    		success: function (msg) {
                    switch(msg){
                        case "1":
							add_num(id);              
                            break
                        case "2":
                            alert("评论失败！");
                            break;
                        case "3":
                           alert("您已经点过了，看看别的吧！");
                            break;
						case "4":
                           alert("请您先登录再进行顶踩操作！");
                            break;
						case "5":
							detle_num(id);
                            break;
                        default:
                            alert("你输入的字符: <<"+ msg +">> 非法");
                            break;
                    }
    		}
    	})
	};

function cartadd (id) {
    	$.ajax({
    		url :'/shoppingcart/ajaxaddCart/',
    		type:'post',
                cache:false,
                data:'id='+id,
    		dataType:'html',
    		success: function (msg) {
				var rep=msg.split(",");
				document.getElementById("ajax_cartnums").innerHTML = rep[0];
				document.getElementById("ajax_cartpicr").innerHTML = rep[1];
    		}
    	})
	};


	function checkLogin(selfurl,id){
	$.ajax({
		url : '/course/checkLogin/',
		type : 'post',
		data:'course_id='+id,
		dataType : 'html',
		success : function (msg){
			if (msg == 1){
				window.location.href = '/users/logReg/'+ selfurl;               
			}else{
				if(msg == 2){
					alert("请先学习该课程，再作评论！");
				}else{
					if(msg == 3){
						alert("您已经评论过，不能再评论！");
					}else{
						var winTop = ($(window).height()/2)-(450/2)+$(document).scrollTop();
						var winLeft = Math.abs($(document).width()-550)/2;
						$('.enterBtn').die('click');
						if($('#course-mark').css('display') == 'none'){
							$('#course-mark').css('display','block');
							$('#course-mark').css('top',winTop+'px');
							$('#course-mark').css('left',winLeft+'px');
							$(".course-mark-on").css("width","130px");
							var courseMarkUl = $('.course-mark-ul > li');
							for(var i =0;i<courseMarkUl.length;i++){
								$(courseMarkUl).eq(i).mouseover(function(){
									var n = $(this).index()+1;
									$(".course-mark-on").css({
										"width":26*n
									});
									var mark_text='';
									if(n>3){
										mark_text='评价：非常好';
									}else if(n>1&&n<=3){
										mark_text='评价：好';
									}else{
										mark_text='评价：一般';
									}
									$('#comment_mark_text').text(mark_text);
								});
							}
							courseMarkUl.click(function(){
								var _i=$(courseMarkUl).index(this);
								$('#comment_mark_valbox').val(_i+1);
							});
							$('.enterBtn').live('click',function(){
								if($(".course-mark-on").width()==0){
									alert('请给课程打分');
									return;
								}else{
									var course_mark = $(".course-mark-on").width()/26;
									var course_id = $("#course_id").val();
									var chapter_id = $("#chapter_id").val();
									var section_id = $("#section_id").val();
									var comment_content = $("#comment_content").val();
									var showV = "";
									if (parseInt(course_mark)>3){
										showV = "评价：非常好";
									}else if (parseInt(course_mark)>1 && parseInt(course_mark)<=3){
										showV = "评价：好";
									}else{
										showV = "评价：一般";
									}
									$('#course-mark').hide();
									$.ajax({

										});
								}
							});
							$('.closeBtn').click(function(){
								$('#course-mark').hide();
							});
						}else{
							return;
						}
					}				
				}	
			}
		},
		error : function (){
			alert('读取数据错误..');
		}
	});
}






/*
 * 详情页进度条 
 */
function progressTips(arrow,progress,box){
	var learn_arrow=arrow;
	var learn_progress=progress;
	var learn_box=box;
	var _temp=learn_arrow.find('li');
	
	var _v1=_temp.eq(0).find('span').text().split('%')[0]-0;
	var _v2=_temp.eq(1).find('span').text().split('%')[0]-0;
	var _v3=_temp.eq(2).find('span').text().split('%')[0]-0;
	//合并1、2项
	var learn_num=[0,_v1+_v2,_v3];
	
	learn_progress.find('li').each(function(i){
		if (learn_num[i] == 0) {
			$(this).width(0);
		}
		else {
			$(this).width(learn_progress.width() * (learn_num[i] / 100) - 3);
		}
	});
	learn_arrow.find('li').each(function(i){
		var _progress=learn_progress.find('li').eq(i);
		if (learn_num[i] == 0) {
			$(this).css({left:_progress.offset().left- learn_box.offset().left-51}).find('span').text(learn_num[i]+'%');
			$(this).hide();
		}
		else {
			$(this).css({
				left: _progress.offset().left + _progress.width() / 2 - 55 - learn_box.offset().left
			}).find('span').text(learn_num[i]+'%');
		}
	});
}

/*
 * 设置进度条箭头指向方式
 * 1. 全部放到上面显示
 * 2. 0%时隐藏
 * 3. 重叠时并排显示
 * 
 */
function progressTipsSetArrow(box){
		var __baseLeft=box.parent('div').offset().left,
			__learn=box.find('li');
		var __a1=__learn.eq(0),	__a2=__learn.eq(1),	__a3=__learn.eq(2);
		var __L1=$(__a1).offset().left,	__L2=$(__a2).offset().left,	__L3=$(__a3).offset().left;
		var __L=0;
			
		if(__L2>__L1&&__L2<(__L1+105)||__L2==__L1){
			__L=__L1+105;
		}else if(__L2<__L3&&(__L2+105)>__L3||__L2==__L3){
			__L=__L3-105;
		}else{
			__L=__L2;
		}
		__a2.css({left:__L-__baseLeft});	
}
/*
 * 给溢出文字添加“……”的插件
 * $().wordLimit(num);	如果无num则根据宽度计算，如果则按照字符数num来计算
 * @param {Object} $
 */
(function($){
	$.fn.wordLimit = function(num){	
		this.each(function(){	
			if(!num){
				var copyThis = $(this.cloneNode(true)).hide().css({
					'position': 'absolute',
					'width': 'auto',
					'overflow': 'visible'
				});	
				$(this).after(copyThis);
				if(copyThis.width()>$(this).width()){
					$(this).text($(this).text().substring(0,$(this).text().length-4));
					$(this).html($(this).html()+'...');
					copyThis.remove();
					$(this).wordLimit();
				}else{
					copyThis.remove(); //清除复制
					return;
				}	
			}else{
				var maxwidth=num;
				if($(this).text().length>maxwidth){
					$(this).text($(this).text().substring(0,maxwidth));
					$(this).html($(this).html()+'...');
				}
			}					 
		});
	}		  
})(jQuery);